package com.haixiaoke.saas.enterprise.order.controller;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.constant.OrderConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.order.domain.OrderAuditRecord;
import com.haixiaoke.saas.order.service.IOrderAuditRecordService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单审核记录Controller
 *
 * @author qixi
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/enterprise/order/auditRecord")
public class EnterpriseOrderAuditRecordController extends BaseController {
    @Autowired
    private IOrderAuditRecordService orderAuditRecordService;

    /**
     * 查询订单审核记录列表
     */
    // @PreAuthorize("@ss.hasPermi('order:auditRecord:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(OrderAuditRecord orderAuditRecord) {
        startPage();
        orderAuditRecord.setTenantId(TenantContext.getTenantId());
        List<OrderAuditRecord> list = orderAuditRecordService.selectOrderAuditRecordList(orderAuditRecord);
        return getDataTable(list);
    }

    /**
     * 查询订单审核记录取消预订的列表
     */
    // @PreAuthorize("@ss.hasPermi('order:auditRecord:list')")
    @GetMapping("/cancelReservation/list")
    @TenantScope
    public TableDataInfo cancelReservationList(OrderAuditRecord orderAuditRecord) {
        startPage();
        orderAuditRecord.setTenantId(TenantContext.getTenantId());
        orderAuditRecord.setAuditRecordType(OrderConstants.AUDIT_RECORD_TYPE_CANCEL_RESERVATION);
        List<OrderAuditRecord> list = orderAuditRecordService.selectAuditcanCelReservationList(orderAuditRecord);
        return getDataTable(list);
    }

    /**
     * 导出订单审核记录列表
     */
    @PreAuthorize("@ss.hasPermi('order:auditRecord:export')")
    @Log(title = "订单审核记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderAuditRecord orderAuditRecord) {
        List<OrderAuditRecord> list = orderAuditRecordService.selectOrderAuditRecordList(orderAuditRecord);
        ExcelUtil<OrderAuditRecord> util = new ExcelUtil<OrderAuditRecord>(OrderAuditRecord.class);
        util.exportExcel(response, list, "订单审核记录数据");
    }

    /**
     * 获取订单审核记录详细信息
     */
    // @PreAuthorize("@ss.hasPermi('order:auditRecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return success(orderAuditRecordService.selectOrderAuditRecordByRecordId(recordId));
    }

    /**
     * 获取退订订单审核记录详细信息
     */
    // @PreAuthorize("@ss.hasPermi('order:auditRecord:query')")
    @GetMapping(value = "/cancelReservation/{recordId}")
    public AjaxResult cancelReservationGetInfo(@PathVariable("recordId") Long recordId) {
        return success(orderAuditRecordService.selectOrderCancelReservationGetInfoById(recordId));
    }

    // /**
    //  * 新增订单审核记录
    //  */
    // @PreAuthorize("@ss.hasPermi('order:auditRecord:add')")
    // @Log(title = "订单审核记录", businessType = BusinessType.INSERT)
    // @PostMapping
    // public AjaxResult add(@RequestBody OrderAuditRecord orderAuditRecord) {
    //     return toAjax(orderAuditRecordService.insertOrderAuditRecord(orderAuditRecord));
    // }

    /**
     * 订单审核
     */
    // @PreAuthorize("@ss.hasPermi('order:auditRecord:edit')")
    @Log(title = "订单审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderAuditRecord orderAuditRecord) {
        return toAjax(orderAuditRecordService.reviewOrder(orderAuditRecord));
    }

    /**
     * 取消预订订单审核
     */
    // @PreAuthorize("@ss.hasPermi('order:auditRecord:edit')")
    @Log(title = "订单审核", businessType = BusinessType.UPDATE)
    @PutMapping("/cancelReservation")
    public AjaxResult cancelReservationEdit(@RequestBody OrderAuditRecord orderAuditRecord) {
        if (StrUtil.equals(OrderConstants.AUDIT_RECORD_STATE_PASS,orderAuditRecord.getAuditRecordState())) {
            orderAuditRecord.setAuditProcess(OrderConstants.AUDIT_PROCESS_REFUND);
        } else if (StrUtil.equals(OrderConstants.AUDIT_RECORD_STATE_NOT,orderAuditRecord.getAuditRecordState())){
            orderAuditRecord.setAuditProcess(OrderConstants.AUDIT_PROCESS_REJECTED);
        }
        orderAuditRecord.setOperatorId(getUserId());
        return orderAuditRecordService.reviewCancelReservationOrder(orderAuditRecord);
    }

    /**
     * 删除订单审核记录
     */
    @PreAuthorize("@ss.hasPermi('order:auditRecord:remove')")
    @Log(title = "订单审核记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(orderAuditRecordService.deleteOrderAuditRecordByRecordIds(recordIds));
    }
}
